package com.aube.plugin.episode;

import com.huyn.baseframework.model.ChannelDetail;
import com.huyn.baseframework.model.VideoDefinition;
import com.huyn.baseframework.model.VideoWithDefinitionModel;
import com.huyn.baseframework.utils.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by huyaonan on 16/5/17.
 */
public class VideoModel {

    public static String SHOWID = "showid";
    public static String VIDEOID = "videoid";
    public static String VIDEOTITLE = "videoTitle";
    public static String VIDEODESC = "videoDesc";
    public static String PLAYTIME = "playTimestamp";
    public static String DURATION = "duration";
    public static String VIDEONO = "videono";
    public static String PICINFO = "picinfo";
    public static String PICURL = "picurl";
    public static String SUBTITLE = "subtitle";
    public static String CAMERATYPE = "cameraType";
    public static String UPDOWNANGLE = "updownAngle";
    public static String LEFTRIGHTANGLE = "leftRightAngle";

    public String showid;
    public String videoid;
    public String videoTitle;
    public String videoDesc;
    public String playTimestamp;
    public int duration;
    public String videono;
    public String pic;
    public String subtitle;
    public String cameraType;
    public String updownAngle;
    public String leftRightAngle;

    public VideoWithDefinitionModel videoPlay;

    public void setItem(String key, Object value) {
        if(value == null)
            return;
        try {
            if (SHOWID.equalsIgnoreCase(key)) {
                showid = (String) value;
            } else if (VIDEOID.equalsIgnoreCase(key)) {
                videoid = (String) value;
            } else if (VIDEOTITLE.equalsIgnoreCase(key)) {
                videoTitle = (String) value;
            } else if (VIDEODESC.equalsIgnoreCase(key)) {
                videoDesc = (String) value;
            } else if (PLAYTIME.equalsIgnoreCase(key)) {
                playTimestamp = (String) value;
            } else if (SUBTITLE.equalsIgnoreCase(key)) {
                subtitle = (String) value;
            } else if (DURATION.equalsIgnoreCase(key)) {
                duration = (int) ((double) value);
            } else if (VIDEONO.equalsIgnoreCase(key)) {
                videono = (String) value;
            } else if (CAMERATYPE.equalsIgnoreCase(key)) {
                cameraType = (String) value;
            } else if (UPDOWNANGLE.equalsIgnoreCase(key)) {
                updownAngle = (String) value;
            } else if (LEFTRIGHTANGLE.equalsIgnoreCase(key)) {
                leftRightAngle = (String) value;
            } else if (PICINFO.equalsIgnoreCase(key)) {
                LinkedHashMap<String, Object> set = (LinkedHashMap<String, Object>) value;
                if (set.containsKey(PICURL))
                    pic = (String) set.get(PICURL);
            } else if("videoPlay".equalsIgnoreCase(key)) {
                videoPlay = new VideoWithDefinitionModel();
                LinkedHashMap<String, Object> set = (LinkedHashMap<String, Object>) value;
                if(set.containsKey("defaultdefinition")) {
                    videoPlay.defaultdefinition = (int) ((double) set.get("defaultdefinition"));
                }
                if(set.containsKey("definitionDescription")) {
                    List<Object> videos = (List<Object>) set.get("definitionDescription");
                    if(videos != null) {
                        List<String> definitions = new ArrayList<>();
                        for(Object o : videos)
                            definitions.add((String) o);
                        videoPlay.definitionDescription = definitions;
                    }
                }
                if(set.containsKey("definitions")) {
                    List<Object> list = (List<Object>) set.get("definitions");
                    if(list != null) {
                        List<VideoDefinition> definitionList = new ArrayList<>();
                        for(Object o : list) {
                            LinkedHashMap<String, Object> item = (LinkedHashMap<String, Object>) o;
                            if(item != null) {
                                VideoDefinition definition = new VideoDefinition();
                                definition.definition = (int) ((double) item.get("definition"));
                                definition.desp = (String) item.get("desp");
                                definition.playurl = (String) item.get("playurl");
                                definitionList.add(definition);
                            }
                        }
                        videoPlay.definitions = definitionList;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isLegal() {
        if(StringUtils.isNotBlank(videono))
            return true;
        if(videoPlay != null && videoPlay.definitions != null) {
            VideoDefinition definition  = videoPlay.getByIndex(0);
            if(definition != null && StringUtils.isNotBlank(definition.playurl))
                return true;
        }
        return false;
    }

    public ChannelDetail toChannelDetail() {
        ChannelDetail cDetail = new ChannelDetail();
        cDetail.duration = this.duration;
        cDetail.playTimestamp = this.playTimestamp;
        cDetail.showid = this.showid;
        cDetail.videoDesc = this.videoDesc;
        cDetail.videoid = this.videoid;
        cDetail.videono = this.videono;
        cDetail.videoTitle = this.videoTitle;
        cDetail.cameraType = this.cameraType;
        cDetail.leftRightAngle = this.leftRightAngle;
        cDetail.updownAngle = this.updownAngle;
        cDetail.videoPlay = this.videoPlay;
        return cDetail;
    }

}
